library(haven)
library(AER)
library(clusterSEs)
library(sandwich)
library(tidyverse)
library(stargazer)
library(Amelia)
install.packages("gridExtra")
library(gridExtra)

setwd("C:/Users/dapon/Dropbox/Gov2001 Replication/Original replication download/results/maps/data")



#################
#MAP REPLICATION# - figure 3 in paper
#################

rm(list=ls())

library(sp)
library(rgeos)
install.packages("ggrepel")
library(ggrepel)
library(maptools)

# Turkey #
########## 
tur <- readRDS("TUR_adm0.rds") 
tur <- fortify(tur, region="OBJECTID")
tur$treatment <- -1
tur$treatment <- factor(tur$treatment, labels="not in Sample")
tur$NL_NAME_3 <- "Turkey"
tur$treatarrivals2_cut <- -1

# Greece #
##########
grc <- readRDS("GRC_adm3.rds") 
grc <- fortify(grc, region="OBJECTID")

n <- read_csv("arrivals.csv")
m <- merge(grc, n, by.x="id", by.y="OBJECTID")

m <- m %>% mutate(treatment=ifelse(is.na(treatment), -1, treatment), 
                  treatarrivals2_cut=cut(treatarrivals2, c(-0.01, 0.5, 1, 2, 3, 4, Inf), labels=FALSE)-1, 
                  treatarrivals2_cut=ifelse(is.na(treatarrivals2_cut),-1, treatarrivals2_cut) )
m <- m %>%  mutate(treatment=factor(treatment, labels=c("not in Sample", "No", "Yes"))) 
m <- ungroup(m)

# Hotspots #
############

hotspot <- read_csv("hotspots.csv") 
islands <- read_csv("islands.csv") 

# Plot #
########
cols_cut <- c("#f0f0f0", "#74c476", "#fdae6b", "#fd8d3c", "#f16913", "#d94801", "#a63603")

p1 <- ggplot() + geom_polygon(data=m, aes(x=long, y=lat, group=group, fill=treatarrivals2_cut)) + 
  scale_fill_gradientn( colors=cols_cut, breaks=c(-1, 0,1,2,3,4,5),
                        labels=c("not in sample", "0", "(0,1]", "(1,2]", "(2,3]", "(3,4]", "> 4"), guide='legend') + 
  coord_fixed(xlim=c(23,29),ylim=c(35,40)) +
  geom_polygon(data=tur, aes(x=long, y=lat, group=group, fill=treatarrivals2_cut)) + 
  guides(fill = guide_legend(title.position="top", ticks = FALSE, 
                             title="Refugee arrivals per capita", nrow=1)) + 
  theme_minimal(base_size=9)  + 
  theme(legend.position='bottom', 
        legend.key.size = unit(0.4, "cm")) + 
  annotate("text", x = 28.75, y = 39.75, label = "Turkey", col='#737373', size=2.5) + 
  annotate("text", x = 23.25, y = 38.25, label = "Greece", col='#737373', size=2.5) + 
  ylab("Latitude") + xlab("Longitude")

setwd("C:/Users/dapon/Dropbox/Gov2001 Replication/Original replication download/")

ggsave("fig_arrivalmap.pdf", p1,
       width=(6.3228344444445/3)*2.5, 
       height=(6.3228344444445/3)*2.5, 
       units="in")
